
<!DOCTYPE html>
<html lang="en">
    <head><meta name="generator" content="Hexo 3.9.0">
    
        <title>FairyGUI - 在Lua中使用</title>
    
        <meta charset="utf-8">
        <meta name="description" content="FairyGUI教程">
        <meta name="keywords" content="FairyGUI,FGUI,FairyGUI教程">
        
        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">

        <meta property="og:type" content="article">
        <meta property="og:title" content="-在Lua中使用FairyGUI">
        <meta property="og:description" content="FairyGUI教程">

        <meta name="twitter:card" content="summary">
        <meta name="twitter:title" content="在Lua中使用 — FairyGUI">
        <meta name="twitter:description" content="FairyGUI教程">

      <!--  <link href='//fonts.proxy.ustclug.org/css?family=Source+Sans+Pro:300,400,600|Roboto Mono' rel='stylesheet' type='text/css'> -->

        <!-- main page styles -->
        <link rel="stylesheet" href="/docs/css/page.css">

        <!-- this needs to be loaded before guide's inline scripts -->
        <script src="/docs/js/vue.min.js"></script>
        <script>window.PAGE_TYPE = "guide_unity"</script>
    </head>
    <body class="docs">        <div id="mobile-bar" data-bg-text="FairyGUI">
            <a class="menu-button"></a>
        </div>
        <div id="header">
  <ul id="nav">
    <li><a href="/docs/guide/" class="nav-link current">教程</a></li>
<li><a href="/docs/examples/" class="nav-link">在线示例</a></li>
<li><a href="/docs/release_notes/" class="nav-link">发行日志</a></li>
<li><a href="/" class="nav-link">首页</a></li>
  </ul>
</div>

        
            <div id="main" class="fix-sidebar">
                
                    
    <div class="sidebar">
    <ul class="main-menu">
        <li><a href="/docs/guide/" class="nav-link current">教程</a></li>
<li><a href="/docs/examples/" class="nav-link">在线示例</a></li>
<li><a href="/docs/release_notes/" class="nav-link">发行日志</a></li>
<li><a href="/" class="nav-link">首页</a></li>
    </ul>
    <div class="list">
        <ul class="menu-root">
            

	 			
	                
	                <li>
	                    <a href="/docs/guide/index.html" class="sidebar-link">导读</a>
	                </li>
	            
	
	            
	                
	                <li>
	                    <a href="/docs/guide/editor/index.html" class="sidebar-link">编辑器使用基础</a>
	                </li>
	            
	                
	                <li>
	                    <a href="/docs/guide/editor/project_settings.html" class="sidebar-link">项目设置</a>
	                </li>
	            
	                
	                <li>
	                    <a href="/docs/guide/editor/package.html" class="sidebar-link">包</a>
	                </li>
	            
	                
	                <li>
	                    <a href="/docs/guide/editor/branch.html" class="sidebar-link">分支</a>
	                </li>
	            
	                
	                <li>
	                    <a href="/docs/guide/editor/publish.html" class="sidebar-link">发布</a>
	                </li>
	            
	                
	                <li>
	                    <a href="/docs/guide/editor/object.html" class="sidebar-link">元件</a>
	                </li>
	            
	                
	                <li>
	                    <a href="/docs/guide/editor/image.html" class="sidebar-link">图片</a>
	                </li>
	            
	                
	                <li>
	                    <a href="/docs/guide/editor/movieclip.html" class="sidebar-link">动画</a>
	                </li>
	            
	                
	                <li>
	                    <a href="/docs/guide/editor/graph.html" class="sidebar-link">图形</a>
	                </li>
	            
	                
	                <li>
	                    <a href="/docs/guide/editor/loader.html" class="sidebar-link">装载器</a>
	                </li>
	            
	                
	                <li>
	                    <a href="/docs/guide/editor/text.html" class="sidebar-link">文本</a>
	                </li>
	            
	                
	                <li>
	                    <a href="/docs/guide/editor/richtext.html" class="sidebar-link">富文本</a>
	                </li>
	            
	                
	                <li>
	                    <a href="/docs/guide/editor/group.html" class="sidebar-link">组</a>
	                </li>
	            
	                
	                <li>
	                    <a href="/docs/guide/editor/component.html" class="sidebar-link">组件</a>
	                </li>
	            
	                
	                <li>
	                    <a href="/docs/guide/editor/scrollpane.html" class="sidebar-link">滚动容器</a>
	                </li>
	            
	                
	                <li>
	                    <a href="/docs/guide/editor/controller.html" class="sidebar-link">控制器</a>
	                </li>
	            
	                
	                <li>
	                    <a href="/docs/guide/editor/relation.html" class="sidebar-link">关联系统</a>
	                </li>
	            
	                
	                <li>
	                    <a href="/docs/guide/editor/label.html" class="sidebar-link">标签</a>
	                </li>
	            
	                
	                <li>
	                    <a href="/docs/guide/editor/button.html" class="sidebar-link">按钮</a>
	                </li>
	            
	                
	                <li>
	                    <a href="/docs/guide/editor/combobox.html" class="sidebar-link">下拉框</a>
	                </li>
	            
	                
	                <li>
	                    <a href="/docs/guide/editor/progressbar.html" class="sidebar-link">进度条</a>
	                </li>
	            
	                
	                <li>
	                    <a href="/docs/guide/editor/slider.html" class="sidebar-link">滑动条</a>
	                </li>
	            
	                
	                <li>
	                    <a href="/docs/guide/editor/scrollbar.html" class="sidebar-link">滚动条</a>
	                </li>
	            
	                
	                <li>
	                    <a href="/docs/guide/editor/list.html" class="sidebar-link">列表</a>
	                </li>
	            
	                
	                <li>
	                    <a href="/docs/guide/editor/tree.html" class="sidebar-link">树</a>
	                </li>
	            
	                
	                <li>
	                    <a href="/docs/guide/editor/popup.html" class="sidebar-link">Popup</a>
	                </li>
	            
	                
	                <li>
	                    <a href="/docs/guide/editor/dragdrop.html" class="sidebar-link">Drag&Drop</a>
	                </li>
	            
	                
	                <li>
	                    <a href="/docs/guide/editor/window.html" class="sidebar-link">窗口系统</a>
	                </li>
	            
	                
	                <li>
	                    <a href="/docs/guide/editor/transition.html" class="sidebar-link">动效</a>
	                </li>
	            
	                
	                <li>
	                    <a href="/docs/guide/editor/adaptation.html" class="sidebar-link">适配</a>
	                </li>
	            
	                
	                <li>
	                    <a href="/docs/guide/editor/i18n.html" class="sidebar-link">多国语言</a>
	                </li>
	            
	                
	                <li>
	                    <a href="/docs/guide/editor/export.html" class="sidebar-link">导入和导出</a>
	                </li>
	            
	                
	                <li>
	                    <a href="/docs/guide/editor/preference.html" class="sidebar-link">偏好设置</a>
	                </li>
	            
	                
	                <li>
	                    <a href="/docs/guide/editor/plugin.html" class="sidebar-link">插件</a>
	                </li>
	            
	
	            
	                
	                
	                <li><h3>SDK</h3></li>
	                
	                <li>
	                    <a href="/docs/guide/sdk/laya.html" class="sidebar-link">LayaAir</a>
	                </li>
	            
	                
	                
	                <li>
	                    <a href="/docs/guide/sdk/egret.html" class="sidebar-link">Egret</a>
	                </li>
	            
	                
	                
	                <li>
	                    <a href="/docs/guide/sdk/cocos2dx.html" class="sidebar-link">Cocos2dx</a>
	                </li>
	            
	                
	                
	                <li>
	                    <a href="/docs/guide/sdk/creator.html" class="sidebar-link">Cocos Creator</a>
	                </li>
	            
	                
	                
	                <li>
	                    <a href="/docs/guide/sdk/cryengine.html" class="sidebar-link">Cry Engine</a>
	                </li>
	            

	            
	                
	                
	                <li><h3>SDK - Unity</h3></li>
	                
	                <li>
	                    <a href="/docs/guide/unity/index.html" class="sidebar-link">显示UI面板</a>
	                </li>
	            
	                
	                
	                <li>
	                    <a href="/docs/guide/unity/transform.html" class="sidebar-link">坐标系统</a>
	                </li>
	            
	                
	                
	                <li>
	                    <a href="/docs/guide/unity/atlas.html" class="sidebar-link">纹理集的处理</a>
	                </li>
	            
	                
	                
	                <li>
	                    <a href="/docs/guide/unity/font.html" class="sidebar-link">字体的处理</a>
	                </li>
	            
	                
	                
	                <li>
	                    <a href="/docs/guide/unity/input.html" class="sidebar-link">输入处理</a>
	                </li>
	            
	                
	                
	                <li>
	                    <a href="/docs/guide/unity/event.html" class="sidebar-link">事件机制</a>
	                </li>
	            
	                
	                
	                <li>
	                    <a href="/docs/guide/unity/insert3d.html" class="sidebar-link">插入模型/粒子/Spine/Canvas</a>
	                </li>
	            
	                
	                
	                <li>
	                    <a href="/docs/guide/unity/uipainter.html" class="sidebar-link">曲面UI</a>
	                </li>
	            
	                
	                
	                <li>
	                    <a href="/docs/guide/unity/drawcall.html" class="sidebar-link">DrawCall优化</a>
	                </li>
	            
	                
	                
	                <li>
	                    <a href="/docs/guide/unity/lua.html" class="sidebar-link current">在Lua中使用</a>
	                </li>
	            
	                
	                
	                <li>
	                    <a href="/docs/guide/unity/special.html" class="sidebar-link">特色功能</a>
	                </li>
	            
	                
	                
	                <li>
	                    <a href="/docs/guide/unity/faq.html" class="sidebar-link">常见问题</a>
	                </li>
	            

            
        </ul>
    </div>
</div>


<div class="content guide_unity with-sidebar ">
    
      <h1>在Lua中使用</h1>
    
    <h2 id="准备工作"><a href="#准备工作" class="headerlink" title="准备工作"></a>准备工作</h2><ol>
<li><strong>如果你使用的是ToLua，在Scripting Define Symbols里加上</strong> <code>FAIRYGUI_TOLUA</code>。XLUA则不需要。</li>
<li><p>如果是ToLua，将以下语句添加到CustomSettings.cs适当的位置，然后重新生成绑定文件。</p>
<figure class="highlight csharp"><table><tr><td class="code"><pre><span class="line"></span><br><span class="line">_GT(<span class="keyword">typeof</span>(EventContext)),</span><br><span class="line">_GT(<span class="keyword">typeof</span>(EventDispatcher)),</span><br><span class="line">_GT(<span class="keyword">typeof</span>(EventListener)),</span><br><span class="line">_GT(<span class="keyword">typeof</span>(InputEvent)),</span><br><span class="line">_GT(<span class="keyword">typeof</span>(DisplayObject)),</span><br><span class="line">_GT(<span class="keyword">typeof</span>(Container)),</span><br><span class="line">_GT(<span class="keyword">typeof</span>(Stage)),</span><br><span class="line">_GT(<span class="keyword">typeof</span>(FairyGUI.Controller)),</span><br><span class="line">_GT(<span class="keyword">typeof</span>(GObject)),</span><br><span class="line">_GT(<span class="keyword">typeof</span>(GGraph)),</span><br><span class="line">_GT(<span class="keyword">typeof</span>(GGroup)),</span><br><span class="line">_GT(<span class="keyword">typeof</span>(GImage)),</span><br><span class="line">_GT(<span class="keyword">typeof</span>(GLoader)),</span><br><span class="line">_GT(<span class="keyword">typeof</span>(GMovieClip)),</span><br><span class="line">_GT(<span class="keyword">typeof</span>(TextFormat)),</span><br><span class="line">_GT(<span class="keyword">typeof</span>(GTextField)),</span><br><span class="line">_GT(<span class="keyword">typeof</span>(GRichTextField)),</span><br><span class="line">_GT(<span class="keyword">typeof</span>(GTextInput)),</span><br><span class="line">_GT(<span class="keyword">typeof</span>(GComponent)),</span><br><span class="line">_GT(<span class="keyword">typeof</span>(GList)),</span><br><span class="line">_GT(<span class="keyword">typeof</span>(GRoot)),</span><br><span class="line">_GT(<span class="keyword">typeof</span>(GLabel)),</span><br><span class="line">_GT(<span class="keyword">typeof</span>(GButton)),</span><br><span class="line">_GT(<span class="keyword">typeof</span>(GComboBox)),</span><br><span class="line">_GT(<span class="keyword">typeof</span>(GProgressBar)),</span><br><span class="line">_GT(<span class="keyword">typeof</span>(GSlider)),</span><br><span class="line">_GT(<span class="keyword">typeof</span>(PopupMenu)),</span><br><span class="line">_GT(<span class="keyword">typeof</span>(ScrollPane)),</span><br><span class="line">_GT(<span class="keyword">typeof</span>(Transition)),</span><br><span class="line">_GT(<span class="keyword">typeof</span>(UIPackage)),</span><br><span class="line">_GT(<span class="keyword">typeof</span>(Window)),</span><br><span class="line">_GT(<span class="keyword">typeof</span>(GObjectPool)),</span><br><span class="line">_GT(<span class="keyword">typeof</span>(Relations)),</span><br><span class="line">_GT(<span class="keyword">typeof</span>(RelationType)),</span><br><span class="line">_GT(<span class="keyword">typeof</span>(Timers)),</span><br><span class="line">_GT(<span class="keyword">typeof</span>(GTween)),</span><br><span class="line">_GT(<span class="keyword">typeof</span>(GTweener)),</span><br><span class="line">_GT(<span class="keyword">typeof</span>(EaseType)),</span><br><span class="line">_GT(<span class="keyword">typeof</span>(TweenValue)),</span><br><span class="line">_GT(<span class="keyword">typeof</span>(UIObjectFactory)),</span><br></pre></td></tr></table></figure>
</li>
<li><p>将FairyGUI.lua放入你的lua文件存放目录。</p>
</li>
</ol>
<h2 id="监听事件"><a href="#监听事件" class="headerlink" title="监听事件"></a>监听事件</h2><ol>
<li><p>普通方法的侦听和删除侦听</p>
<figure class="highlight csharp"><table><tr><td class="code"><pre><span class="line"></span><br><span class="line">require <span class="string">'FairyGUI'</span></span><br><span class="line"></span><br><span class="line"><span class="function">function <span class="title">OnClick</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">	<span class="title">print</span>(<span class="params"><span class="string">'you click'</span></span>)</span></span><br><span class="line"><span class="function">end</span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function">--也可以带上事件参数</span></span><br><span class="line"><span class="function">function <span class="title">OnClick</span>(<span class="params">context</span>)</span></span><br><span class="line"><span class="function">	<span class="title">print</span>(<span class="params"><span class="string">'you click'</span>..context.sender</span>)</span></span><br><span class="line"><span class="function">end</span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function">UIPackage.<span class="title">AddPackage</span>(<span class="params"><span class="string">'Demo'</span></span>)</span></span><br><span class="line"><span class="function">local view</span> = UIPackage.CreateObject(<span class="string">'Demo'</span>, <span class="string">'DemoMain'</span>)</span><br><span class="line">GRoot.inst:AddChild(view)</span><br><span class="line"></span><br><span class="line">view.onClick:Add(OnClick)</span><br><span class="line">--view.onClick:Remove(OnClick)</span><br><span class="line">--view.onClick:Set(OnClick)</span><br></pre></td></tr></table></figure>
</li>
</ol>
<ol start="2">
<li><p><strong>ToLua支持带self的回调</strong></p>
<figure class="highlight csharp"><table><tr><td class="code"><pre><span class="line"></span><br><span class="line">function TestClass:OnClick()</span><br><span class="line">	print(<span class="string">'you click'</span>)</span><br><span class="line">end</span><br><span class="line"></span><br><span class="line">function TestClass:OnClick(context)</span><br><span class="line">	print(<span class="string">'you click'</span>..context.sender)</span><br><span class="line">end</span><br><span class="line"></span><br><span class="line">self.view.onClick:Add(TestClass.OnClick, self)</span><br><span class="line">self.view.onClick:Remove(TestClass.OnClick, self)</span><br></pre></td></tr></table></figure>
</li>
</ol>
<h2 id="使用Window类"><a href="#使用Window类" class="headerlink" title="使用Window类"></a>使用Window类</h2><p>FairyGUI提供的Window类，一般需要开发者自己扩展，例如覆盖OnShown,OnHide等。在Lua里，编写Window扩展的方法是：</p>
<figure class="highlight csharp"><table><tr><td class="code"><pre><span class="line">WindowBase = fgui.window_class()</span><br><span class="line"></span><br><span class="line">--构建函数</span><br><span class="line">function WindowBase:ctor()</span><br><span class="line">end</span><br><span class="line"></span><br><span class="line">--可覆盖的函数(可选，不是说必须）</span><br><span class="line">function WindowBase:OnInit()</span><br><span class="line">    self.contentPane = UIPackage.CreateObject(<span class="string">"Basics"</span>, <span class="string">"WindowA"</span>);</span><br><span class="line">end</span><br><span class="line"></span><br><span class="line">function WindowBase:OnShown()</span><br><span class="line">end</span><br><span class="line"></span><br><span class="line">function WindowBase:OnHide()</span><br><span class="line">end</span><br><span class="line"></span><br><span class="line">function WindowBase:DoShowAnimation()</span><br><span class="line">    self:OnShown();</span><br><span class="line">end</span><br><span class="line"></span><br><span class="line">function WindowBase:DoHideAnimation()</span><br><span class="line">    self:HideImmediately();</span><br><span class="line">end</span><br><span class="line"></span><br><span class="line">--创建并显示窗口</span><br><span class="line">local win = WindowBase.New();</span><br><span class="line">win:Show();</span><br><span class="line"></span><br><span class="line">也可以继续继承以上得到的Window类，例如:</span><br><span class="line">MyWindow = fgui.window_class(WindowBase)</span><br><span class="line"></span><br><span class="line">在继承类里调用父类方法的:</span><br><span class="line">function MyWindow:OnInit()</span><br><span class="line">	WindowBase.OnInit(self)</span><br><span class="line">end</span><br></pre></td></tr></table></figure>
<h2 id="自定义扩展"><a href="#自定义扩展" class="headerlink" title="自定义扩展"></a>自定义扩展</h2><p>FairyGUI在C#里可以使用<code>UIObjectFactory.SetPackageItemExtension</code>进行自定义扩展。在Lua里，同样可以这样做。方法如下：</p>
<ol>
<li><p>定义扩展类。注意基础类型，不要搞错。例如按钮是GButton，一般的组件则是GComponent。</p>
<figure class="highlight csharp"><table><tr><td class="code"><pre><span class="line"></span><br><span class="line">MyButton = fgui.extension_class(GButton)</span><br><span class="line"></span><br><span class="line">--注意这里不是构造函数，是当组件已经构建完毕后调用的</span><br><span class="line"></span><br><span class="line">function MyButton:ctor()</span><br><span class="line">	print(self:GetChild(<span class="string">'n1'</span>))</span><br><span class="line">end</span><br><span class="line"></span><br><span class="line">--添加自定义的方法和字段</span><br><span class="line">function MyButton:Test()</span><br><span class="line">	print(<span class="string">'test'</span>)</span><br><span class="line">end</span><br><span class="line"></span><br><span class="line">local <span class="keyword">get</span> = tolua.initget(MyButton)</span><br><span class="line">local <span class="keyword">set</span> = tolua.initset(MyButton)</span><br><span class="line"><span class="keyword">get</span>.myProp = function(self)</span><br><span class="line">	<span class="keyword">return</span> self._myProp</span><br><span class="line">end</span><br><span class="line"></span><br><span class="line"><span class="keyword">set</span>.myProp = function(self, <span class="keyword">value</span>)</span><br><span class="line">	self._myProp = <span class="keyword">value</span></span><br><span class="line">	self:GetChild(<span class="string">'n1'</span>).text = <span class="keyword">value</span></span><br><span class="line">end</span><br></pre></td></tr></table></figure>
</li>
<li><p>注册扩展类。要在创建任何对象前注册好。</p>
<figure class="highlight csharp"><table><tr><td class="code"><pre><span class="line">fgui.register_extension(<span class="string">"ui://包名/我的按钮"</span>, MyButton)</span><br></pre></td></tr></table></figure>
</li>
<li><p>完成以上两步后，任何“我的按钮”这个资源创建出来的对象都可以使用MyButton访问了。例如：</p>
<figure class="highlight csharp"><table><tr><td class="code"><pre><span class="line"></span><br><span class="line">local myButton = someComponent:GetChild(<span class="string">"myButton"</span>) --这个myButton的资源是“我的按钮”</span><br><span class="line">myButton:Test()</span><br><span class="line">myButton.myProp = <span class="string">'hello'</span></span><br><span class="line"></span><br><span class="line">local myButton2 = UIPackage.CreateObject(<span class="string">"包名"</span>,<span class="string">"我的按钮"</span>)</span><br><span class="line">myButton2:Test()</span><br><span class="line">myButton2.myProp = <span class="string">'world'</span></span><br></pre></td></tr></table></figure></li>
</ol>

    
    <div class="footer">
      发现错误或想贡献文档?
      <a href="https://github.com/fairygui/fairygui.github.io/tree/master/src/guide/unity/lua.md" target="_blank">
        在 Github 上编辑此文档!
      </a>
    </div>
</div>

                
            </div>
            <script src="/docs/js/smooth-scroll.min.js"></script>
        

        <!-- main custom script for sidebars, version selects etc. -->        
        <script src="/docs/js/css.escape.js"></script>
        <script src="/docs/js/common.js"></script>
    </body>
</html>
